package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.common.enums.FilePaths;
import com.ywqf_api.dao.RentForSaleDao;
import com.ywqf_api.dto.in.RentForSaleDto;
import com.ywqf_api.entity.Customer;
import com.ywqf_api.entity.RentSale;
import com.ywqf_api.service.RentForSaleService;
import com.ywqf_api.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Administrator on 2018/4/3 0003.
 */
@Service
public class RentForSaleServiceImpl implements RentForSaleService {
    @Autowired
    private RentForSaleDao rentForSaleDao;

    @Override
    @Transactional
    public int rentOrSale(RentForSaleDto rentForSaleDto) {
        String num = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                num += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                num += String.valueOf(random.nextInt(10));
            }
        }
        Integer type = rentForSaleDto.getType();
        Integer houseId = rentForSaleDto.getHouseId();
        Integer customerId = rentForSaleDto.getCustomerId();
        Customer customer = rentForSaleDao.findCustomer(customerId);
        if(customer == null){
            return 0;
        }
        if(customer.getName().equals(null)){
            rentForSaleDto.setName("无");
        }
        rentForSaleDto.setName(customer.getName());
        rentForSaleDto.setPhone(customer.getTelephone());
        rentForSaleDto.setNum(num);
        String[]a = rentForSaleDto.getUrl().split(",");
        if(type == 1){                      //type 1 出租  2 出售
            Double rentPrice = rentForSaleDto.getRentPrice();
            RentSale rentSale = rentForSaleDao.findIsNull(houseId);
                if(rentSale == null){
                    int save = rentForSaleDao.saveRent(rentForSaleDto);
                    if(save > 0){
                        Integer houseSellRentId = rentForSaleDto.getId();
                      for(int i=0; i<a.length;i++){
                          String img = a[i];
                          int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                          if(saveImg < 1){
                              throw new BaseException("添加失败");
                          }
                      }
                      return 1;
                    }else{
                        throw new BaseException("添加失败");
                    }
                }else{
                    if(rentSale.getType() == 3){
                        int update = rentForSaleDao.updateRents(rentForSaleDto);
                        if(update > 0){
                            Integer id = rentSale.getId();
                            int delete = rentForSaleDao.deleteUrl(id);
                            if(delete < 1){
                                throw new BaseException("删除图片失败");
                            }
                            Integer houseSellRentId = rentSale.getId();
                            for(int i=0; i<a.length;i++){
                                String img = a[i];
                                int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                                if(saveImg < 1){
                                    throw new BaseException("添加失败");
                                }
                            }
                            return 11;
                        }else{
                            throw new BaseException("编辑失败");
                        }
                    }else if (rentSale.getType() == 1){
                        rentForSaleDto.setType(3);
                    }else if (rentSale.getType() == 2){
                        rentForSaleDto.setType(2);
                    }
                    Integer id = rentSale.getId();
                    int delete = rentForSaleDao.deleteUrl(id);
                    if(delete < 1){
                        throw new BaseException("删除图片失败");
                    }
                    int update = rentForSaleDao.updateRent(rentForSaleDto);
                    if(update > 0){
                        Integer houseSellRentId = rentSale.getId();
                        for(int i=0; i<a.length;i++){
                            String img = a[i];
                            int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                            if(saveImg < 1){
                                throw new BaseException("添加失败");
                            }
                        }
                        return 11;
                    }else{
                        throw new BaseException("编辑失败");
                    }
                }
        }else{
            Double salePrice = rentForSaleDto.getSalePrice();
            RentSale rentSale = rentForSaleDao.findIsNull(houseId);
            if(rentSale == null){
                int save = rentForSaleDao.saveSale(rentForSaleDto);
                if(save > 0){
                    Integer houseSellRentId = rentForSaleDto.getId();
                    for(int i=0; i<a.length;i++){
                        String img = a[i];
                        int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(saveImg < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 1;
                }else{
                    throw new BaseException("添加失败");
                }
            }else{
                if(rentSale.getType() == 3){
                    int updates = rentForSaleDao.updateSales(rentForSaleDto);
                    if(updates > 0){
                        Integer id = rentSale.getId();
                        int delete = rentForSaleDao.deleteUrl(id);
                        if(delete < 1){
                            throw new BaseException("删除图片失败");
                        }
                        Integer houseSellRentId = rentSale.getId();
                        for(int i=0; i<a.length;i++){
                            String img = a[i];
                            int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                            if(saveImg < 1){
                                throw new BaseException("添加失败");
                            }
                        }
                        return 11;
                    }else {
                        throw new BaseException("编辑失败");
                    }
                }else if (rentSale.getType() == 1){
                    rentForSaleDto.setType(1);
                }else if (rentSale.getType() == 2){
                    rentForSaleDto.setType(3);
                }
                Integer id = rentSale.getId();
                int delete = rentForSaleDao.deleteUrl(id);
                if(delete < 1){
                    throw new BaseException("删除图片失败");
                }
                int update = rentForSaleDao.updateSale(rentForSaleDto);
                if(update > 0){
                    Integer houseSellRentId = rentSale.getId();
                    for(int i=0; i<a.length;i++){
                        String img = a[i];
                        int saveImg = rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(saveImg < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 11;
                }else{
                    throw new BaseException("编辑失败");
                }
            }
        }
    }

    @Override
    public int rentOrSaleFile(RentForSaleDto rentForSaleDto) {
        String num = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                num += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                num += String.valueOf(random.nextInt(10));
            }
        }
        Integer type = rentForSaleDto.getType();
        Integer houseId = rentForSaleDto.getHouseId();
        Integer customerId = rentForSaleDto.getCustomerId();
        Customer customer = rentForSaleDao.findCustomer(customerId);
        if(customer == null){
            return 0;
        }
        if(customer.getName().equals(null)){
            rentForSaleDto.setName("无");
        }
        rentForSaleDto.setName(customer.getName());
        rentForSaleDto.setPhone(customer.getTelephone());
        rentForSaleDto.setNum(num);
    /*    String[]a = rentForSaleDto.getUrl().split(",");*/
        if(type == 1){                      //type 1 出租  2 出售
            Double rentPrice = rentForSaleDto.getRentPrice();
            RentSale rentSale = rentForSaleDao.findIsNull(houseId);
            if(rentSale == null){
                int save = rentForSaleDao.saveRent(rentForSaleDto);
                if(save > 0){
                    Integer houseSellRentId = rentForSaleDto.getId();
                    for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                        MultipartFile file = rentForSaleDto.getFile().get(o);
                        String img = UploadUtil.saveFile(file, FilePaths.USER);
                        img = "developer/"+img;
                        int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(add < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 1;
                }else{
                    throw new BaseException("添加失败");
                }
            }else{
                if(rentSale.getType() == 3){
                    int update = rentForSaleDao.updateRents(rentForSaleDto);
                    if(update > 0){
                        Integer id = rentSale.getId();
                        int delete = rentForSaleDao.deleteUrl(id);
                  /*      if(delete < 1){
                            throw new BaseException("删除图片失败");
                        }*/
                        Integer houseSellRentId = rentSale.getId();
                        for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                            MultipartFile file = rentForSaleDto.getFile().get(o);
                            String img = UploadUtil.saveFile(file, FilePaths.USER);
                            img = "developer/"+img;
                            int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                            if(add < 1){
                                throw new BaseException("添加失败");
                            }
                        }
                        return 11;
                    }else{
                        throw new BaseException("编辑失败");
                    }
                }else if (rentSale.getType() == 1){
                    rentForSaleDto.setType(3);
                }else if (rentSale.getType() == 2){
                    rentForSaleDto.setType(2);
                }
                Integer id = rentSale.getId();
                int delete = rentForSaleDao.deleteUrl(id);
            /*    if(delete < 1){
                    throw new BaseException("删除图片失败");
                }*/
                int update = rentForSaleDao.updateRent(rentForSaleDto);
                if(update > 0){
                    Integer houseSellRentId = rentSale.getId();
                    for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                        MultipartFile file = rentForSaleDto.getFile().get(o);
                        String img = UploadUtil.saveFile(file, FilePaths.USER);
                        img = "developer/"+img;
                        int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(add < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 11;
                }else{
                    throw new BaseException("编辑失败");
                }
            }
        }else{
            Double salePrice = rentForSaleDto.getSalePrice();
            RentSale rentSale = rentForSaleDao.findIsNull(houseId);
            if(rentSale == null){
                int save = rentForSaleDao.saveSale(rentForSaleDto);
                if(save > 0){
                    Integer houseSellRentId = rentForSaleDto.getId();
                    for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                        MultipartFile file = rentForSaleDto.getFile().get(o);
                        String img = UploadUtil.saveFile(file, FilePaths.USER);
                        img = "developer/"+img;
                        int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(add < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 1;
                }else{
                    throw new BaseException("添加失败");
                }
            }else{
                if(rentSale.getType() == 3){
                    int updates = rentForSaleDao.updateSales(rentForSaleDto);
                    if(updates > 0){
                        Integer id = rentSale.getId();
                        int delete = rentForSaleDao.deleteUrl(id);
                      /*  if(delete < 1){
                            throw new BaseException("删除图片失败");
                        }*/
                        Integer houseSellRentId = rentSale.getId();
                        for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                            MultipartFile file = rentForSaleDto.getFile().get(o);
                            String img = UploadUtil.saveFile(file, FilePaths.USER);
                            img = "developer/"+img;
                            int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                            if(add < 1){
                                throw new BaseException("添加失败");
                            }
                        }
                        return 11;
                    }else {
                        throw new BaseException("编辑失败");
                    }
                }else if (rentSale.getType() == 1){
                    rentForSaleDto.setType(1);
                }else if (rentSale.getType() == 2){
                    rentForSaleDto.setType(3);
                }
                Integer id = rentSale.getId();
                int delete = rentForSaleDao.deleteUrl(id);
        /*        if(delete < 1){
                    throw new BaseException("删除图片失败");
                }*/
                int update = rentForSaleDao.updateSale(rentForSaleDto);
                if(update > 0){
                    Integer houseSellRentId = rentSale.getId();
                    for(int o=0; o<rentForSaleDto.getFile().size(); o++){
                        MultipartFile file = rentForSaleDto.getFile().get(o);
                        String img = UploadUtil.saveFile(file, FilePaths.USER);
                        img = "developer/"+img;
                        int add =rentForSaleDao.saveRentImg(houseSellRentId,img);
                        if(add < 1){
                            throw new BaseException("添加失败");
                        }
                    }
                    return 11;
                }else{
                    throw new BaseException("编辑失败");
                }
            }
        }
    }

    @Override
    public Map findRentSaleData(Integer houseId) {
        RentSale rentSale = rentForSaleDao.findRentSaleData(houseId);
        if(rentSale == null){
            return null;
        }
        if(rentSale.getReleaseTime().length()>=19){
            rentSale.setReleaseTime(rentSale.getReleaseTime().substring(0,19));
        }
        if(rentSale.getOperateTime().length()>=19){
            rentSale.setOperateTime(rentSale.getOperateTime().substring(0,19));
        }
        Integer id = rentSale.getId();
        List<RentSale> url = rentForSaleDao.findRentSaleUrl(id);
        Map map = new HashMap();
        map.put("data",rentSale);
        map.put("url",url);
        return map;
    }
}
